package com.magist.manage.copyrights.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.magist.core.entity.CxtHolder;
import com.magist.core.entity.Page;
import com.magist.core.service.SimpleServiceImpl;
import com.magist.core.util.BeanCompareUtil;
import com.magist.core.util.BeanCopyUtil;
import com.magist.core.util.EnumOrderUtil;
import com.magist.manage.copyrights.dao.CommunicationMapper;
import com.magist.manage.copyrights.dto.CommunicationDto;
import com.magist.manage.copyrights.entity.Communication;
import com.magist.manage.copyrights.entity.SupplyInfo;
import com.magist.manage.copyrights.entity.SupplyLinkMans;
import com.magist.manage.copyrights.service.CommunicationService;
import com.magist.manage.copyrights.service.SupplyInfoService;
import com.magist.manage.copyrights.service.SupplyLinkMansService;
import com.magist.manage.enums.CommTypeEnum;
import com.magist.manage.enums.OpActionEnum;
import com.magist.manage.enums.OpTypeEnum;
import com.magist.manage.enums.TFEnum;
import com.magist.manage.util.BusinessLogUtil;
import com.magist.manage.web.entity.UserInfo;
import com.magist.manage.web.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CommunicationServiceImpl extends SimpleServiceImpl<Communication> implements CommunicationService {

    @Autowired
    private CommunicationMapper communicationMapper;
    @Autowired
    private SupplyInfoService supplyInfoService;
    @Autowired
    private SupplyLinkMansService supplyLinkMansService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private BusinessLogUtil businessLogUtil;

    @Override
    public PageInfo<Communication> page(Example example, Page page) {
        String orderBy = page.getSort() + " " + page.getOrder();
        if (StringUtils.isEmpty(orderBy.trim())) {
            PageHelper.startPage(page.getCurrPage(), page.getLimit());
        } else {
            PageHelper.startPage(page.getCurrPage(), page.getLimit(), orderBy);
        }
        List<Communication> list = this.find(example);
        PageInfo<Communication> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Communication> page(Communication entity, Page page) {
        String orderBy = page.getSort() + " " + page.getOrder();
        if (StringUtils.isEmpty(orderBy.trim())) {
            PageHelper.startPage(page.getCurrPage(), page.getLimit());
        } else {
            PageHelper.startPage(page.getCurrPage(), page.getLimit(), orderBy);
        }
        List<Communication> list = this.find(entity);
        PageInfo<Communication> pageInfo = new PageInfo(list);
        return pageInfo;
    }


    @Override
    public void doSaveCommunication(Communication communication) {
        communication.setStatus(TFEnum.T.getCode());
        communication.setCommType(EnumOrderUtil.orderEnum(CommTypeEnum.values(), communication.getCommType()));
        if (communication.getSupplyId() != null) {
            SupplyInfo supplyInfo = supplyInfoService.getById(communication.getSupplyId());
            if (supplyInfo != null) {
                communication.setSupplyName(supplyInfo.getNickName());
            }
        }
        if (communication.getLinkNameId() != null) {
            SupplyLinkMans supplyLinkMans = supplyLinkMansService.getById(communication.getLinkNameId());
            if (supplyLinkMans != null) {
                communication.setLinkName(supplyLinkMans.getLinkName());
            }
        }
        if (communication.getObjId() != null) {
            UserInfo userInfo = userInfoService.getById(communication.getObjId());
            if (userInfo != null) {
                communication.setObjName(userInfo.getUserName());
            }
        }
        if (CxtHolder.getUser() != null) {
            communication.setModifier(CxtHolder.getUser().getUserName());
        }
        if (!StringUtils.isEmpty(communication.getId())) {
            CommunicationDto oldDto = BeanCopyUtil.copy(this.getById(communication.getId()), CommunicationDto.class);


            try {
                String compare = BeanCompareUtil.compare(oldDto, BeanCopyUtil.copy(communication, CommunicationDto.class));
                businessLogUtil.recordBusinessLog(OpActionEnum.EDIT.getCode(), OpTypeEnum.Communication.getCode(), communication.getId(), "修改沟通:" + communication.getSupplyName(), compare);
            } catch (Exception e) {
                e.printStackTrace();
            }

            this.update(communication);
        } else {
            communication.setId(this.getNewId());
            this.insert(communication);

            try {
                String info = BeanCompareUtil.info(BeanCopyUtil.copy(communication, CommunicationDto.class));
                businessLogUtil.recordBusinessLog(OpActionEnum.ADD.getCode(), OpTypeEnum.Communication.getCode(), communication.getId(), "新增沟通:" + communication.getSupplyName(), info);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public boolean doUpdateCommunication(Communication communication) {
        if (communication.getSupplyId() != null) {
            SupplyInfo supplyInfo = supplyInfoService.getById(communication.getSupplyId());
            if (supplyInfo != null) {
                communication.setSupplyName(supplyInfo.getNickName());
            }
        }
        if (communication.getLinkNameId() != null) {
            SupplyLinkMans supplyLinkMans = supplyLinkMansService.getById(communication.getLinkNameId());
            if (supplyLinkMans != null) {
                communication.setLinkName(supplyLinkMans.getLinkName());
            }
        }
        if (communication.getObjId() != null) {
            UserInfo userInfo = userInfoService.getById(communication.getObjId());
            if (userInfo != null) {
                communication.setObjName(userInfo.getUserName());
            }
        }
        if (CxtHolder.getUser() != null) {
            communication.setModifier(CxtHolder.getUser().getUserName());
        }
        String id = communication.getId();
        CommunicationDto oldDto = BeanCopyUtil.copy(this.getById(id), CommunicationDto.class);
        if (this.update(communication) > 0) {
            try {
                String compare = BeanCompareUtil.compare(oldDto, BeanCopyUtil.copy(communication, CommunicationDto.class));
                businessLogUtil.recordBusinessLog(OpActionEnum.EDIT.getCode(), OpTypeEnum.Communication.getCode(), communication.getId(), "修改沟通:" + communication.getSupplyName(), compare);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int insert(Communication entity) {
        if (CxtHolder.getUser() != null) {
            entity.setModifier(CxtHolder.getUser().getUserName());
        }
        return super.insert(entity);
    }

    @Override
    public int update(Communication entity) {
        if (CxtHolder.getUser() != null) {
            entity.setModifier(CxtHolder.getUser().getUserName());
        }
        return super.update(entity);
    }

    @Override
    public List<Communication> findByCreateById(String userId) {
        if(StringUtils.isEmpty(userId)) {
            return null;
        }
        Communication communication = new Communication();
        communication.setCreateById(userId);
        return this.find(communication);
    }
}